/******************************************************************
* Create dataset for analyzing outcomes of incumbents

*******************************************************************/

cap log close
cap log using "$logs/create_inclevel_dataset", replace 

* Load
use "$files/incumbent_panel.dta", clear
keep fakeid_worker fakeid_estab year treat blind blstate blmr *work ///
 gender blage childbearing blten blocc emp1231 educ ///
 earningsavgnom1 contractsal contractsaltype contracthours ///
 atblemp atdiffemployer nonCUTemployer notindata promoteblemp ///
 blemp blfem blmal blempinv blempl contract_id union_id 

* Schooling
// convert education into years of schooling
gen schooling = .
replace schooling = 0 if educ==1
replace schooling = 2 if educ==2
replace schooling = 4 if educ==3
replace schooling = 6 if educ==4
replace schooling = 8 if educ==5
replace schooling = 9 if educ==6
replace schooling = 11 if educ==7
replace schooling = 13 if educ==8
replace schooling = 15 if educ==9
replace schooling = 17 if educ==10
replace schooling = 19 if educ==11
//schooling at baseline
gen xxx = schooling if year==2014
gegen blschool = max(xxx), by(fakeid_worker)
drop xxx

* December employment at baseline
gen xxx = emp1231 if year==2014
gegen bldecemp = max(xxx), by(fakeid_worker)
drop xxx
 
 * Wage in levels
gen wage = .
//use the average wage from the main year-spell
local agrp "2011 2012 2013 2014 2015 2016 2017"
local bgrp "78.9699590730652 83.5814316074726 88.5216652900615 94.1945931899808 104.246981522556 110.802019433334 114.067821777584"
//use December CPI to write in real terms (base year = 2015)
local n : word count `agrp'
forvalues i = 1/`n' {
	local a : word `i' of `agrp'
	local b : word `i' of `bgrp'
	replace wage = 100*(earningsavgnom1/`b') if year==`a'
}

* Wage in logs 
gen lnwage = log(wage)

keep fakeid_worker fakeid_estab year treat blind blstate blmr *work ///
 gender blage childbearing blten blocc blschool bldecemp ///
 lnwage ///
 atblemp atdiffemployer nonCUTemployer notindata promoteblemp ///
 blemp blfem blmal blempinv blempl contract_id union_id
 
xtset fakeid_worker year
mdesc
save "$files/inclevel_dataset.dta", replace

cap log close
